<template>
  <div class="app-container">
    <el-card class="query-card" v-show="showSearch">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
        <el-form-item label="文件夹名称" prop="folderName">
          <el-input
            v-model="queryParams.folderName"
            placeholder="请输入文件夹名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="文件夹编码" prop="folderCode">
          <el-input
            v-model="queryParams.folderCode"
            placeholder="请输入文件夹编码"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="table-card">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['file:folder:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="info"
            plain
            icon="el-icon-sort"
            size="mini"
            @click="toggleExpandAll"
          >展开/折叠</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <vxe-table
        :loading="loading"
        resizable
        row-key
        ref="tableRef"
        :row-config="{isHover: true}"
        v-if="refreshTable"
        :data="folderList"
        :class="showSearch ? 'table-container' : 'table-container-full'"
        :tree-config="{
          transform: true,
          rowField: 'folderCode',
          parentField: 'parentCode',
          line: true,
          expandAll: isExpandAll,
          iconOpen: 'vxe-icon-folder-open',
          iconClose: 'vxe-icon-folder'
        }"
      >
        <vxe-column title="文件夹名称" field="folderName" min-width="150" tree-node/>
        <vxe-column title="文件夹编码" field="folderCode" min-width="180" align="center"/>
        <vxe-column title="完整文件夹编码路径" field="pathCode" min-width="180" header-align="center" align="left"/>
        <vxe-column title="完整文件夹名称路径" field="pathName" min-width="180" header-align="center" align="left"/>
        <vxe-column title="排序" field="orderNum" align="center" min-width="80" />
        <vxe-column title="备注" field="remark" align="center" min-width="180" />
        <vxe-column title="创建时间" field="createTime" align="center" min-width="150">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </vxe-column>
        <vxe-column title="操作" align="center" fixed="right" min-width="220">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['file:folder:edit']"
              v-if="scope.row.folderCode !== '/'"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-plus"
              @click="handleAdd(scope.row)"
              v-hasPermi="['file:folder:add']"
            >新增</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['file:folder:remove']"
              v-if="scope.row.folderCode !== '/'"
            >删除</el-button>
          </template>
        </vxe-column>
      </vxe-table>
    </el-card>

    <!-- 添加或修改文件夹对话框 -->
    <el-dialog :title="title" :visible.sync="open" v-if="open" v-drag-zoom append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row class="row-content">
          <el-col :span="12">
            <el-form-item label="文件夹名称" prop="folderName">
              <el-input v-model="form.folderName" placeholder="请输入文件夹名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="文件夹编码" prop="folderCode">
              <el-input v-model="form.folderCode" placeholder="请输入文件夹编码" :disabled="this.form.id !== '' && this.form.id !== null" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="父文件夹" prop="parentCode">
              <treeselect v-model="form.parentCode" :options="folderOptions" :normalizer="normalizer" placeholder="请选择父文件夹编码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排序" prop="orderNum">
              <el-input-number v-model="form.orderNum" controls-position="right" :min="0" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" rows="3" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFolder, getFolder, delFolder, addFolder, updateFolder } from "@/api/file/folder";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Folder",
  components: {
    Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 文件夹表格数据
      folderList: [],
      // 文件夹树选项
      folderOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        folderName: null,
        folderCode: null,
        parentCode: null,
        pathCode: null,
        pathName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        folderName: [
          {
            required: true,
            message: '请输入文件夹名称',
            trigger: 'blur'
          }
        ],
        folderCode: [
          {
            required: true,
            message: '请输入文件夹编码',
            trigger: 'blur'
          }
        ],
        parentCode: [
          {
            required: true,
            message: '请选择上级目录',
            trigger: 'blur'
          }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询文件夹列表 */
    getList() {
      this.loading = true;
      listFolder(this.queryParams).then(response => {
        this.folderList = response.data;
        this.loading = false;
        this.$refs.tableRef.reloadData(this.folderList);
      });
    },
    /** 转换文件夹数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.folderCode,
        label: node.folderName,
        children: node.children
      };
    },
	/** 查询文件夹下拉树结构 */
    getTreeselect() {
      listFolder().then(response => {
        this.folderOptions = this.handleTree(response.data, "folderCode", "parentCode");;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        folderName: null,
        folderCode: null,
        parentCode: null,
        pathCode: null,
        pathName: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
        remark: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.getTreeselect();
      if (row != null && row.folderCode) {
        this.form.parentCode = row.folderCode;
      } else {
        this.form.parentCode = '/';
      }
      this.open = true;
      this.title = "添加文件夹";
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      if (row != null) {
        this.form.parentCode = row.folderCode;
      }
      getFolder(row.id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改文件夹";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateFolder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFolder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除文件夹[' + row.folderName + ']?').then(function() {
        return delFolder(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>
<style lang="scss" scoped>
::v-deep .table-container {
  .vxe-table {
    height: calc(100vh - 274px);
  }
  .vxe-table--body-wrapper {
    height: calc(100vh - 308px);
  }
  .fixed-left--wrapper,
  .fixed-right--wrapper {
    height: calc(100vh - 317px);
  }
}
::v-deep .table-container-full {
  .vxe-table {
    height: calc(100vh - 186px);
  }
  .vxe-table--body-wrapper {
    height: calc(100vh - 230px);
  }
  .fixed-left--wrapper,
  .fixed-right--wrapper {
    height: calc(100vh - 239px);
  }
}
</style>
